package com.mycompany.inventory.persistence;

import com.mycompany.common.AbstractDao;
import com.mycompany.common.jdbc.ConnectionDatabase;
import com.mycompany.inventory.pojo.Inventory;

import java.util.ArrayList;
import java.util.List;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by My Company.
 * User: loitran
 * Date: Feb 29, 2012
 * Time: 1:53:19 PM
 * com.mycompany.inventory.persistence.
 */
public class InventoryDAO extends AbstractDao<Inventory>{
	
	private Connection con = this.connect;
	public static final String SELECT_ALL_SQL = "select * from oce_inventory";	
	public static final String SELECT_BY_PRODUCT_SQL = "SELECT a.*, b.quantity FROM oce_inventory a, oce_product_inventory b, oce_product c WHERE c.product_id = b.product_id AND a.inventory_id = b.inventory_id AND b.product_id = ?";
	public static final String SELECT_SQL = "select * from oce_inventory where inventory_id = ?";
	public static final String INSERT_SQL = "INSERT INTO oce_inventory (inventory_id,inventory_name,inventory_locationId) VALUES (?,?,?)";
	
	
	public Inventory create(Inventory obj){
		try{
			PreparedStatement ps = con.prepareStatement(INSERT_SQL, PreparedStatement.RETURN_GENERATED_KEYS);	
			long id = 0l;
			ps.setLong(1,id);
			ps.setString(2, obj.getName());
			ps.setInt(3, obj.getLocation());				
			ConnectionDatabase.executeUpdate(ps);	
			ResultSet rs=ps.getGeneratedKeys();
            if(rs.next()){
                Integer Id = (Integer) rs.getInt(1);               
                obj.setId(String.valueOf(Id));
            }
			ps.close();			
			
		}catch (Exception e) {
			// TODO: handle exception
			obj = null;
		}
		return obj;
	}
	
	public Inventory update(Inventory obj){
		try{
			
		}catch (Exception e) {
			// TODO: handle exception
		}
		return obj;
	}
	
	public Inventory find(long id){
		List<Inventory> inventories = new ArrayList<Inventory>();
		try{
			PreparedStatement ps = con.prepareStatement(SELECT_SQL, PreparedStatement.RETURN_GENERATED_KEYS);
			ps.setLong(1,id);
			inventories = convert(ConnectionDatabase.executeQuery(ps));
		}catch (Exception e) {
			// TODO: handle exception
		}
		return inventories.size()== 0 ? null : inventories.get(0);
	}
	
	public List<Inventory> findAll(){
		List<Inventory> inventories = new ArrayList<Inventory>();
		try{
			inventories = convert(ConnectionDatabase.executeQuery(con.prepareStatement(SELECT_ALL_SQL, PreparedStatement.RETURN_GENERATED_KEYS)));
		}catch (Exception e) {
			// TODO: handle exception
			inventories = null;
		}
		return inventories;
	}
	
	public List<Inventory> findByProductID(long id){
		List<Inventory> inventories = new ArrayList<Inventory>();
		try{
			PreparedStatement ps = con.prepareStatement(SELECT_BY_PRODUCT_SQL, PreparedStatement.RETURN_GENERATED_KEYS);
			ps.setLong(1,id);
			inventories = convertSpecial(ConnectionDatabase.executeQuery(ps));
		}catch (Exception e) {
			// TODO: handle exception
		}
		return inventories;
	}
	
	
	
	public void delete(Inventory obj){
		try{
			
		}catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	private List<Inventory> convert(ResultSet rs) throws SQLException {
		List<Inventory> inventories = new ArrayList<Inventory>();
		while (rs.next()) {
	        // Get the data from the row using the column index
	        String id = rs.getString(1);
	        String name = rs.getString(2);
	        int locationId = rs.getInt(3);	        
	        inventories.add(new Inventory(id,name,locationId));	      
	    }
		rs.close();
		return inventories;
	}
	
	private List<Inventory> convertSpecial(ResultSet rs) throws SQLException {
		List<Inventory> inventories = new ArrayList<Inventory>();
		while (rs.next()) {
	        // Get the data from the row using the column index
	        String id = rs.getString(1);
	        String name = rs.getString(2);
	        int locationId = rs.getInt(3);	        
	        int quantity = rs.getInt(6);	
	        inventories.add(new Inventory(id,name,locationId,quantity));
	    }
		rs.close();
		return inventories;
	}
	
       
	

}
